package com.wisezone.java1902.d12_conections;

public class MyLinkedList<E> {
	class Node{
		E data;
		Node pointer;
		public Node(E element) {
			this.data = element;
		}
	}
	private Node header = null; //头节点
	private Node next = null; //当前操作节点
	private int size = 0; //元素个数
	//添加元素
	public void add(E item) {
		Node node = new Node(item); //新节点
		size++;
		//判断当前节点是否头节点
		if(header == null) {
			header = node;
			header.pointer = null;
			next = header;
			return;
		}
		next.pointer = node;
		next = node;

	}
	//获取元素
	public E get(int index) {
		Node next = header;
		E data = null;
		for (int i = 0; i < size; i++) {
			if(index == i) {
				data= next.data;
				break;
			}
			next = next.pointer;
		}
		return data;
	}
	
	//元素个数
	public int size() {
		return size;
	}
	
	public static void main(String[] args) {
		MyLinkedList<String> lists = new MyLinkedList<String>();
		lists.add("元素一");
		lists.add("元素二");
		lists.add("元素三");
		
		String item = lists.get(2);
		System.out.println("item :"+item);
	}
}
